package com.beyondsoft.dtc.mobile.automation.uiautomator.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import com.beyondsoft.dtc.mobile.automation.uiautomator.model.UiDumpModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * UIAutomator xml解析器. 目前只存储了常见的属性.
 *
 * @author scottche
 * @version 1.0 2016.4.19 初版创立.
 */
public class UiDumpXmlFileParser extends DefaultHandler {
	/**
	 * 解析xml文件中的"node"元素.
	 */
	public final static String NODE_NAME = "node";
	/**
	 * "node"元素的text属性.
	 */
	public final static String ATTRIBUTE_TEXT = "text";
	/**
	 * "node"元素的resource属性.
	 */
	public final static String ATTRIBUTE_RESOURCE_ID = "resource-id";
	/**
	 * "node"元素的class属性.
	 */
	public final static String ATTRIBUTE_CLASS = "class";
	/**
	 * "node"元素的content-desc属性.
	 */
	public final static String ATTRIBUTE_CONTENT_DESC = "content-desc";
	/**
	 * "node"元素的checkable属性.
	 */
	public final static String ATTRIBUTE_CHECKABLE = "checkable";
	/**
	 * "node"元素的checkable属性.
	 */
	public final static String ATTRIBUTE_CHECKED = "checked";
	/**
	 * "node"元素的clickable属性.
	 */
	public final static String ATTRIBUTE_CLICKABLE = "clickable";
	/**
	 * "node"元素的clickable属性.
	 */
	public final static String ATTRIBUTE_BOUNDS = "bounds";
	/**
	 * "node"元素的clickable属性.
	 */
	public final static String ATTRIBUTE_PASSWORD = "password";
	/*
	 * "node"元素的package属性
	 */
	public final static String ATTRIBUTE_PACKAGE = "package";
	final Logger logger = LoggerFactory.getLogger(UiDumpXmlFileParser.class);
	/**
	 * 解析后元素集合
	 */
	private List<UiDumpModel> dumps = new ArrayList<UiDumpModel>();

	/**
	 * 解析UIAutoMator生成的画面元素xml文件存储并返回.
	 *
	 * @param xml
	 * @return 解析成功：解析后的元素结合 解析失败:null.
	 */
	public List<UiDumpModel> getDumps(InputStream xml) {
		boolean isSuccess = false;

		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = null;

		try {
			parser = factory.newSAXParser();
			parser.parse(xml, this);
			isSuccess = true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return this.dumps;
	}

	@Override
	public void startDocument() throws SAXException {
		dumps = new ArrayList<UiDumpModel>();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// 遇到Node元素的场合.
		if (NODE_NAME.equals(qName)) {
			UiDumpModel dump = new UiDumpModel();
			dump.setText(attributes.getValue(ATTRIBUTE_TEXT));
			dump.setResourceId(attributes.getValue(ATTRIBUTE_RESOURCE_ID));
			dump.setClassName(attributes.getValue(ATTRIBUTE_CLASS));
			dump.setContentDesc(attributes.getValue(ATTRIBUTE_CONTENT_DESC));
			dump.setCheckable(attributes.getValue(ATTRIBUTE_CHECKABLE));
			dump.setChecked(attributes.getValue(ATTRIBUTE_CHECKED));
			dump.setClickable(attributes.getValue(ATTRIBUTE_CLICKABLE));
			dump.setBounds(attributes.getValue(ATTRIBUTE_BOUNDS));
			dump.setPassword(attributes.getValue(ATTRIBUTE_PASSWORD));
			dump.setPackageName(attributes.getValue(ATTRIBUTE_PACKAGE));
			dumps.add(dump);
			dump.setIndex(dumps.size() - 1);
		}
	}
}
